class Solution {
public:
    string longestPalindrome(string s) {
        int len = 1;
        int n = s.size();
        int begin = 0;
        vector<vector<bool>> dp(n, vector<bool>(n, 0));
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (s[i] == s[j]) {
                    if (j > i + 1) {
                        dp[i][j] = dp[i + 1][j - 1];
                    }
                    else {
                        dp[i][j] = true;
                    }
                    if (dp[i][j]) {
                        if (len < (j - i + 1)) {
                            len = j - i + 1;
                            begin = i;
                        }
                    }
                }
            }
        }
        return s.substr(begin, len);
    }
};